package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.ClassShopBeen;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.FamilyBuyBeen;
import cn.xfdzx.android.apps.shop.bean.FoundGoodTitleBeen;
import cn.xfdzx.android.apps.shop.bean.HomeMarketInitDataBeen;
import cn.xfdzx.android.apps.shop.bean.HomeMarketSelectBeen;
import cn.xfdzx.android.apps.shop.bean.HomeRetailBeen;
import cn.xfdzx.android.apps.shop.bean.NearShopBeen;
import cn.xfdzx.android.apps.shop.contract.ClassShopContract;
import cn.xfdzx.android.apps.shop.contract.HomeRetailContract;
import cn.xfdzx.android.apps.shop.model.ClassShopModel;
import cn.xfdzx.android.apps.shop.model.HomeRetailModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author
 * @date Description：
 */
public class HomeRetailPresenter extends BasePresenter<HomeRetailContract.View> implements HomeRetailContract.Presenter {

    private HomeRetailContract.Model model;

    public HomeRetailPresenter() {
        model = new HomeRetailModel();
    }

    @Override
    public void initData() {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }

        model.initData().compose(RxScheduler.<HomeRetailBeen>Flo_io_main())
                .as(mView.<HomeRetailBeen>bindAutoDispose()).subscribe(new Consumer<HomeRetailBeen>() {
            @Override
            public void accept(HomeRetailBeen baseObjectBean) throws Exception {

                mView.onInitDataSuccess(baseObjectBean);

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

                mView.onError(throwable);

            }
        });
    }

    @Override
    public void familyBuy(String sales_model) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }

        model.familyBuy(sales_model).compose(RxScheduler.<FamilyBuyBeen>Flo_io_main())
                .as(mView.<FamilyBuyBeen>bindAutoDispose()).subscribe(new Consumer<FamilyBuyBeen>() {
            @Override
            public void accept(FamilyBuyBeen baseObjectBean) throws Exception {

                mView.onFamilyBuySuccess(baseObjectBean);

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

                mView.onError(throwable);

            }
        });
    }


    @Override
    public void addCar(String is_new, String uniqueId, String productId, String cartNum,int position,int total_num) {
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.addCar(is_new,uniqueId,productId,cartNum) .compose(RxScheduler.<ConfirmGoodBeen>Flo_io_main())
                .as(mView.<ConfirmGoodBeen>bindAutoDispose())
                .subscribe(new Consumer<ConfirmGoodBeen>() {
                    @Override
                    public void accept(ConfirmGoodBeen bean) throws Exception {
                        mView.onAddCarSuccess(bean,  position,  total_num);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void initTitle(String pid,String index_status) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }

        model.initTitle(pid,index_status).compose(RxScheduler.<FoundGoodTitleBeen>Flo_io_main())
                .as(mView.<FoundGoodTitleBeen>bindAutoDispose()).subscribe(new Consumer<FoundGoodTitleBeen>() {
            @Override
            public void accept(FoundGoodTitleBeen baseObjectBean) throws Exception {

                mView.onInitTitleSuccess(baseObjectBean);

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

                mView.onError(throwable);

            }
        });
    }

}
